package com.example.finalproject.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.finalproject.MainActivity;
import com.example.finalproject.R;
import com.example.finalproject.utils.DatabaseHelper;

public class UpdateActivity extends AppCompatActivity {
    private EditText editUsernameUpdate, editOldPasswordUpdate, editNewPasswordUpdate;
    private Button buttonUpdatePage;
    private DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        databaseHelper = DatabaseHelper.getInstance(this);
        editUsernameUpdate = findViewById(R.id.editUsernameUpdate);
        editOldPasswordUpdate = findViewById(R.id.editOldPasswordUpdate);
        editNewPasswordUpdate = findViewById(R.id.editNewPasswordUpdate);
        buttonUpdatePage = findViewById(R.id.buttonUpdatePage);

        buttonUpdatePage.setOnClickListener(view -> {
            String username = editUsernameUpdate.getText().toString();
            String oldPassword = editOldPasswordUpdate.getText().toString();
            String newPassword = editNewPasswordUpdate.getText().toString();

            if (databaseHelper.update(username, oldPassword, newPassword) != -1) {
                Toast.makeText(this, "密码修改成功!", Toast.LENGTH_SHORT).show();
                Intent intentAccount = new Intent(this, MainActivity.class);
                intentAccount.putExtra("navigate_to_index", 2);
                startActivity(intentAccount);
                finish();
            } else {
                Toast.makeText(this, "密码修改失败！", Toast.LENGTH_SHORT).show();
            }
        });
    }
}